package com.marvels.approve.process.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.approve.common.utils.PropertiesLoadUtils;
import com.marvels.approve.process.consum.ConsumerExternalFacesigApi;
import com.marvels.approve.process.dao.ITimeConfHistoryDao;
import com.marvels.approve.process.dto.FaceSigAppointmentDTO;
import com.marvels.approve.process.service.FaceSigTaskletApi;
import com.marvels.approve.process.service.IFaceSigAppointmentService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.JSONObject;

@Service
public class FaceSigTaskletApiImpl implements FaceSigTaskletApi {
	@Autowired
	private IFaceSigAppointmentService faceSigAppointmentService;
	@Autowired
	private ConsumerExternalFacesigApi consumerExternalApi;
	@Autowired
	private ITimeConfHistoryDao  timeConfHistoryMapper;
	
	@Override
	public void scanOutmoded() throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info("scanOutmoded==>>开始扫描");
		try {
			faceSigAppointmentService.updateScanOutmoded();
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("scanOutmoded==>>扫描过号信息异常");
		}

	}

	@Override
	public void scanNeedTips() throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info("scanTipmoded==>>开始扫描需要提示的预约号");
		String execHostName = PropertiesLoadUtils.getPropertiesValue("send.msg.hostname", "common.properties");
		String currentHostName = "";
		try {
			currentHostName = (InetAddress.getLocalHost()).getHostName();
		} catch (UnknownHostException e1) {
			MarvelsLogUtil.getInstance().error("get currentHostName error==>>", e1);
		}
		MarvelsLogUtil.getInstance().info("currentHostName==>>" + currentHostName);
		if (!StringUtils.equalsIgnoreCase(execHostName, currentHostName)) {
			return;
		}
		try {
			List<FaceSigAppointmentDTO> list = faceSigAppointmentService.selectNeedTips();
			MarvelsLogUtil.getInstance().info("scanTipmoded==>>发现" + list.size() + "条需要提示");
			for (FaceSigAppointmentDTO faceSigAppointment : list) {
				Map<String, String> pushMap = new HashMap<String, String>();
				pushMap.put("targetAlias", faceSigAppointment.getcId());
				pushMap.put("permeate", faceSigAppointment.getCustName() + "客户预约面签10分钟后开始,请提前做好准备。");
				pushMap.put("loanId", faceSigAppointment.getLoanId());
				pushMap.put("title", "面签提醒");
				pushMap.put("userId", faceSigAppointment.getUserId() + "");
				pushMap.put("type", "1");
				pushMap.put("target", "interview");
				MarvelsLogUtil.getInstance().info("push content pushMap===>>" + JSONObject.fromObject(pushMap));
				String jsonResult = consumerExternalApi.pushFacePush(pushMap);
				MarvelsLogUtil.getInstance().info("push result===>>" + JSONObject.fromObject(jsonResult));
				FaceSigAppointmentDTO faceSigAppointmentParam = new FaceSigAppointmentDTO();
				faceSigAppointmentParam.setId(faceSigAppointment.getId());
				faceSigAppointmentParam.setIsPush(1);
				faceSigAppointmentService.updateAppointment(faceSigAppointmentParam,false);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("scanOutmoded==>>扫描提示处理异常", e);
		}

	}

	@Override
	public void updateTimeConfHistoryStatus() throws MarvelsBusinessException {
		timeConfHistoryMapper.updateTimeConfHistoryStatus();
	}

}
